<script lang="ts" setup name="GoodsInfo">
import GoodsImage from './components/goods-image.vue';
import GoodsSales from './components/goods-sales.vue';
import GoodsName from './components/goods-name.vue';
import GoodsSku from './components/goodsSku.vue';
import GoodsDetail from './components/goods-detail.vue';
import GoodsHot from './components/goods-hot.vue';
import useStore from '@/store';
import {CartItem} from '@/types/cart'
// 导入路由参数对象
import { useRoute } from 'vue-router';
// 侦听
import { ref, watchEffect } from 'vue';
import { storeToRefs } from 'pinia';
import Message from '@/components/message';
const {goods,cart}=useStore()
const route=useRoute()
// watchEffect解决 组件缓存
watchEffect(()=>{
    if(route.fullPath===`/goods/${route.params.id}`){
        goods.getGoodsInfo(route.params.id as string)
    }
})
const {goodsInfo}=storeToRefs(goods)

// sku规格
let currentSkuId=ref('')
const changeSku = (skuId:string) => {
  // 每次点击将传过来的skuId赋值
   currentSkuId.value=skuId
   const sku = goodsInfo.value.skus.find((item)=> item.id === skuId)
   if(sku){
     goodsInfo.value.inventory = sku.inventory //更新库存
     goodsInfo.value.price = sku.price  //更新价格
     goodsInfo.value.oldPrice  = sku.oldPrice  //更新老价格
   }
}
// 购买数量
const count=ref(1)


// 加入购物车
const addCart=async()=>{
  //  console.log(currentSkuId,count);
    if(!currentSkuId){
    return  Message.warning('请选择完整的规格')
    }
 const sku = goodsInfo.value.skus.find(item=>item.id===currentSkuId.value)
//  console.log(sku);
const attrsText = sku?.specs.map(item=>`${item.name}:${item.valueName}`).join(' ')
// console.log(attrsText);


    await cart.joinCart({// 本地添加
    id: goodsInfo.value.id,  //商品Id
    name: goodsInfo.value.name,  //商品名称
    picture: goodsInfo.value.mainPictures[0], //第一张图片地址
    price: goodsInfo.value.price,  //价格
    count: count.value,  //数量
    skuId: currentSkuId.value,  //skuid
    attrsText: attrsText,   //描述
    selected: true,  //是否选中
    nowPrice: goodsInfo.value.price, //现价
    stock: goodsInfo.value.inventory, //库存
    isEffective: true, //是否有效
  } as CartItem)
    Message.success('加入购物车成功')
}
</script>
<template>
  <div class="xtx-goods-page" v-if="goodsInfo.id">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodsInfo?.categories?.[1]?.id}`">{{goodsInfo?.categories?.[1]?.name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodsInfo?.categories?.[0]?.id}`">{{goodsInfo?.categories?.[0]?.name}}</XtxBreadItem>
        <XtxBreadItem >{{goodsInfo.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左侧 -->
       <div class="media">
       <GoodsImage :images="goodsInfo.mainPictures"></GoodsImage>
       <GoodsSales></GoodsSales>
       </div>
       <!-- 右侧 -->
        <div class="spec">
          <GoodsName :goods="goodsInfo"></GoodsName>
          <!-- 区域4 商品规格组件  -->
         <GoodsSku @changeSku="changeSku" :goods="goodsInfo"/>
         <!-- 数量选择 -->
         <XtxNumbox v-model="count"></XtxNumbox>
         <!-- 加入购物车按钮 -->
         <XtxButton type="primary" style="margin-top: 20px" @click="addCart">
      加入购物车
    </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
            <GoodsDetail :goods="goodsInfo"></GoodsDetail>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
           <GoodsHot :type="1" />
           <GoodsHot :type="2" />
           <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
   display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}

</style>
